package LeetCode;

/**
 * @Author Kechelle
 * @Date 2020/9/27 15:20
 * @Version 1.0
 */
public class Code134 {
    public static int canCompleteCircuit(int[] gas, int[] cost) {
        if (gas == null || cost == null ||gas.length == 0 || cost.length == 0){
            return -1;
        }
        //记录剩余的油量
        int remain = 0;
        //总的耗油量
        int total = 0;
        int index = -1;
        for (int i=0;i< gas.length;i++){
            remain = remain + gas[i] - cost[i];
            total = total + gas[i] - cost[i];
            //如果剩余油量为0,从当前位置开始作为起点判断是否能走一圈
            if (remain<0){
                index = i+1;
                remain = 0;
            }
        }
        return total<0? -1:index;
    }

    public static void main(String[] args) {
        int[] gas = {1,2,3,4,5};
        int[] cost = {3,4,5,1,2};
        long startTime = System.currentTimeMillis();
        System.out.println(canCompleteCircuit(gas,cost));
        long endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime);
    }
}
